package com.comdev.ctrls.test;

import com.comdev.common.Page;
import com.comdev.common.Reps;
import com.comdev.common.WebPath;
import com.comdev.exceptions.CommonOut;
import com.comdev.orm.jdbc.DbKit;
import com.comdev.ut.string.StringUT;
import com.comdev.vos.t_news;
import com.dbutils.QueryHelper;
import org.nutz.castor.Castors;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Criteria;
import org.nutz.dao.util.cri.SimpleCriteria;
import org.nutz.lang.Lang;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.util.*;

/**
 * User: zhu
 * Date: 13-4-30
 * Time: 下午5:43
 */
@RequestMapping("curd")
@Controller
public class curd
{
    //显示list页面
    @RequestMapping("show")
    public String show()
    {
        return "test/curd/show";
    }

    //获取图片信息
    @ResponseBody
    @RequestMapping("getimg")
    public  Map getimg()
    {
        Map out = new HashMap<String, String>();
        try
        {

            //获取首页背景图片  上传区域
            String s2 = getimg_html2("1111,22222");
            Reps.add(out, "imgdivs", s2);

            //获取遥控器 说明图片   上传区域
            String s3 = getimg_html("1111");
            Reps.add(out, "img_id", s3);

        } catch (CommonOut e)
        {
            Reps.add(out, e);
            return out;
        } catch (Exception e)
        {
            Reps.add(out, e);
            return out;
        }
        Reps.success(out);
        return out;
    }

    /**
     *  获取多个图片对应的html
     * @param img_ids
     * @return
     */
    private String getimg_html2(String img_ids)
    {
        StringBuilder stringBuilder = new StringBuilder();
        String s = img_ids;

        if (StringUT.isEmpty(s))
        {
            s = "<tr>\n" +
                    "                <th width=\"100\">第" + 1 + "张图片</th>\n" +
                    "                <td width=\"150\">\n" +
                    "                    <div index_=\"" + 1 + "\" id=\"img" + 1 + "\">\n" +
                    "                        <a onclick=\"javascript:flashupload2('img" + 1 + "');\" href=\"javascript:;\">上传</a>\n" +
                    "                    </div>\n" +
                    "                </td>\n" +
                    "                <td><a href=\"javascript:add_line(" + 1 + ")\">+</a></td>\n" +
                    "</tr>";
            return s;
        }

        String[] ss = s.split("\\,");
        List<String> ss_ = new ArrayList<String>();

        for (int i = 0; i < ss.length; i++)
        {
            String filepath = DbKit.getString("select savepath_ from t_file where objid='" + ss[i] + "'");
            if (!StringUT.isEmpty(filepath) && new File(WebPath.getUploadRootPath() + filepath).exists())
            {
                ss_.add(ss[i]);
            }
        }

        if (StringUT.isEmpty(ss_))
        {
            return "<tr>\n" +
                    "                <th width=\"100\">第" + 1 + "张图片</th>\n" +
                    "                <td width=\"150\">\n" +
                    "                    <div index_=\"" + 1 + "\" id=\"img" + 1 + "\">\n" +
                    "                        <a onclick=\"javascript:flashupload2('img" + 1 + "');\" href=\"javascript:;\">上传</a>\n" +
                    "                    </div>\n" +
                    "                </td>\n" +
                    "                <td><a href=\"javascript:add_line(" + 1 + ")\">+</a></td>\n" +
                    "</tr>";
        }

        for (int i = 1; i <= ss_.size(); i++)
        {
            if (i == 1)
            {
                stringBuilder.append("<tr>\n" +
                        "                <th width=\"100\">第" + 1 + "张图片</th>\n" +
                        "                <td width=\"150\">\n" +
                        "                    <div index_=\"" + 1 + "\" id=\"img" + 1 + "\">\n" +
                        "<a href=\"/file/down/" + ss_.get(i - 1) + "\" objid=\"" + ss_.get(i - 1) + "\" target=\"_blank\">查看</a>&nbsp;\n" +
                        "<a href=\"javascript:;\" onclick=\"javascript:delfile('" + ss_.get(i - 1) + "','img" + i + "');\">删除</a>&nbsp;\n" +
                        "<a onclick=\"javascript:flashupload2('img" + 1 + "');\" href=\"javascript:;\">重新上传</a>" +
                        "                    </div>\n" +
                        "                </td>\n" +
                        "                <td><a href=\"javascript:add_line(" + 1 + ")\">+</a></td>\n" +
                        "</tr>");
            } else
            {
                stringBuilder.append("<tr>\n" +
                        "                <th width=\"100\">第" + i + "张图片</th>\n" +
                        "                <td width=\"150\">\n" +
                        "                    <div index_=\"" + i + "\" id=\"img" + i + "\">\n" +
                        "<a href=\"/file/down/" + ss_.get(i - 1) + "\" objid=\"" + ss_.get(i - 1) + "\" target=\"_blank\">查看</a>&nbsp;\n" +
                        "<a href=\"javascript:;\" onclick=\"javascript:delfile('" + ss_.get(i - 1) + "','img" + i + "');\">删除</a>&nbsp;\n" +
                        "<a onclick=\"javascript:flashupload2('img" + i + "');\" href=\"javascript:;\">重新上传</a>" +
                        "                    </div>\n" +
                        "                </td>\n" +
                        "              <td><a href=\"javascript:add_line(" + i + ")\">+</a>&nbsp;&nbsp;<a href=\"javascript:remove_line(" + i + ")\">-</a></td>\n" +
                        "</tr>");
            }
        }
        return stringBuilder.toString();
    }

    /**
     *  获取一张图片对应的 html
     * @param img_id
     * @return
     */
    private String getimg_html(String img_id)
    {
        String imgtxt = "";
        String imgid = img_id;

        if (!StringUT.isEmpty(imgid))
        {
            String filepath = DbKit.getString("select savepath_ from t_file where objid='" + imgid + "'");
            if (!StringUT.isEmpty(filepath) && new File(WebPath.getUploadRootPath() + filepath).exists())
            {
                imgtxt = "<a href=\"/file/down/" + imgid + "\" objid=\"" + imgid + "\" target=\"_blank\">查看</a>&nbsp;\n" +
                        "<a href=\"javascript:;\" onclick=\"javascript:delfile('" + imgid + "','img_id');\">删除</a>&nbsp;\n" +
                        "<a onclick=\"javascript:flashupload2('img_id');\" href=\"javascript:;\">重新上传</a>";
            } else
            {
                imgtxt = "<a href=\"javascript:;\" onclick=\"javascript:flashupload2('img_id');\">上传</a>";
            }
            return imgtxt;
        }
        imgtxt = "<a href=\"javascript:;\" onclick=\"javascript:flashupload2('img_id');\">上传</a>";
        return imgtxt;
    }


    //显示list页面
    @RequestMapping("show2")
    public String show2()
    {
        return "test/curd/show2";
    }


    @RequestMapping("list")
    @ResponseBody
    public Map list(@RequestParam("pageno") int pageno, @RequestParam("args") String args)
    {
        Map out = new HashMap();

        Pager pager = new Pager();
        pager.setPageNumber(pageno);
        pager.setPageSize(5);

        String in = StringUT.Base64_decode_url(args);
        String[] in_arr = in.split("\\{&\\}");

        SimpleCriteria cri = Cnd.cri();
        for (int i = 0; i < in_arr.length; i++)
        {
            String temp = in_arr[i];
            String[] temp_arr = temp.split("\\{=\\}");
            if (temp_arr.length == 1)
            {
                continue;
            }
            cri.where().andLike(temp_arr[0], temp_arr[1]);
        }
        cri.desc("modifyDate_");

        int allcount = QueryHelper.query_Count("select * from t_news "+cri.toString());

        out.put("datas", QueryHelper.query_BeanList_pager(t_news.class, "select * from t_news "+cri.toString(), pager));

        //%s是页码
        Pager.DEFAULT_PAGE_SIZE = pager.getPageSize();//把pagesize放在公共变量里
        out.put("pages", Page.pages(allcount, pageno, "javascript:page(%s,'" + args + "');"));
        return out;

    }


    //显示添加的页面
    @RequestMapping("show_add")
    public String show_add()
    {
        return "test/curd/add";
    }

    //show_add2
    @RequestMapping("show_add2")
    public String show_add2()
    {
        return "test/curd/add2";
    }

    @RequestMapping("show_edit")
    public ModelAndView show_edit(@RequestParam("objid_") String objid_)
    {

        ModelAndView mod = new ModelAndView();
        t_news news=   QueryHelper.query_Bean(t_news.class,"select * from t_news where objid_='"+objid_+"'");

        mod.addObject("news", news);

        mod.setViewName("test/curd/add");
        return mod;
    }

    @RequestMapping("show_edit2")
    public ModelAndView show_edit2(@RequestParam("objid_") String objid_)
    {

        ModelAndView mod = new ModelAndView();
        t_news news=   QueryHelper.query_Bean(t_news.class,"select * from t_news where objid_='"+objid_+"'");

        mod.addObject("news", news);
        mod.addObject("objid",objid_);

        mod.setViewName("test/curd/edit2");
        return mod;
    }

    /**
     *  getinfo
     * @param objid
     * @return
     */
    @ResponseBody
    @RequestMapping("getinfo")
    public Map getinfo(@RequestParam("objid")String objid)
    {
        Map out = new HashMap<String, String>();
        try
        {
            t_news voNews=   QueryHelper.query_Bean(t_news.class,"select * from t_news where objid_='"+objid+"'");
            Reps.add(out,"title",voNews.getTitle_());
            Reps.add(out, "content", voNews.getContent_());
        } catch (CommonOut e)
        {
            Reps.add(out, e);
            return out;
        } catch (Exception e)
        {
            Reps.add(out, e);
            return out;
        }
        Reps.success(out);
        return out;
    }



    //执行添加命令
    @RequestMapping("do_addup")
    @ResponseBody
    public String do_addup(@RequestParam("objid") String objid, @RequestParam("title") String title, @RequestParam("content") String content)
    {
        if (StringUT.isEmpty(objid))
        {
            QueryHelper.insert(t_news.me(title, content));
        }
        else
        {
            QueryHelper.execute("update t_news set modifydate_='"+ Castors.me().castToString(new Date())+"', title_='"+title+"'  ,content_='"+content+"' where objid_='"+objid+"'");
        }
        return "";
    }

    //执行添加命令
    @RequestMapping("do_addup2")
    @ResponseBody
    public Map do_addup2(@RequestParam("objid") String objid, @RequestParam("title") String title, @RequestParam("content") String content)
    {
        Map out = new HashMap<String, String>();
        try
        {
            if (StringUT.isEmpty(objid))
            {
                QueryHelper.insert(t_news.me(title, content));
            }
            else
            {
                DbKit.getDao().update(t_news.class, Chain.make("title_", title).add("content_", content), Cnd.where("objid_", "=", objid));
            }
        } catch (CommonOut e)
        {
            Reps.add(out, e);
            return out;
        } catch (Exception e)
        {
            Reps.add(out, e);
            return out;
        }
        Reps.success(out);
        return out;
    }


    @RequestMapping("do_delete")
    public String do_delete(@RequestParam("objid_") String[] objid_)
    {
        if(StringUT.isEmpty(objid_))
        {
            return "/test/curd/show";
        }
        String ids=StringUT.getInStr(Arrays.asList(objid_));
        QueryHelper.execute("delete from t_news where objid_  in ("+ids+")");

        return "/test/curd/show";
    }

    @RequestMapping("showmessage")
    public ModelAndView showmessage(@RequestParam("type") String type, @RequestParam("data") String data, @RequestParam("href") String href)
    {
        ModelAndView mod = new ModelAndView();

        mod.addObject("type", type);
        mod.addObject("data", StringUT.Base64_decode(data.replace("-", "+").replace("_", "/"), "UTF-8"));
        mod.addObject("href", StringUT.Base64_decode(href.replace("-", "+").replace("_", "/"), "UTF-8"));
        mod.setViewName("/test/curd/showmessage");

        return mod;
    }


}
